/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.arbol;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import org.apache.ddlutils.model.Database;
import pfc.comun.IDEpfc;
import pfc.comun.Proyecto;
import pfc.gui.CGModelEdit;
import pfc.gui.CGModelEdit2;
import pfc.modelocg.CGForm;
import pfc.modelocg.CGField;
import pfc.modelocg.CGModel;
import pfc.modeloer.ERAttrib;
import pfc.modeloer.ERModel;
import pfc.modeloer.EREntity;
import pfc.utils.Utils;

/**
 *
 * @author xandru
 */
public class CGTreeNode extends PFCTreeNode implements ActionListener{
    SecurityTreeNode securityNode;
    FolderTreeNode formsTreeNode;
    CGModel _cgModel;
    ERModel _erModel;
    Database _dbModel;
    private CGModel _cgmodel;

    public CGTreeNode(Proyecto pro, String nombre){
        super(pro,nombre);
        securityNode = new SecurityTreeNode(pro,"Seguridad");
        //this.add(securityNode);
        formsTreeNode = new FolderTreeNode(pro,"Formularios");
        this.add(formsTreeNode);

    }


    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


     public void showProperties() {
                System.out.println("Propiedades de " + this.getClass().getName());
                CGModelEdit cgEdit= new CGModelEdit(this);
                IDEpfc.app.addParaEditar(cgEdit,  "Formularios");
            }


    void buildTree(CGModel cg, ERModel er, Database db) {
        for (CGForm form: cg.getForms())
        {
            FormTreeNode fn = new FormTreeNode(proyecto,form.getName());
            fn.setCGFrom(form);
            fn.setCGModel(cg);
            formsTreeNode.add(fn);
            if (form.getEntity()==null)
            {
                EREntity e = er.findEntity(form.getEntityName());
                form.setEntity(e);

                /*
                for (ERAttrib attrib: e.getAttribs())
                {
                  CGField field = new CGField(attrib);
                  form.addField(field);
                }
                 *
                 */
                
                for (CGField field: form.getFields() )
                {
                    if (field.getAttrib()==null)
                    {
                        ERAttrib att = e.findAttrib(field.getAttribName());
                        field.setAttrib(att);
                    }
                }                
            }
        }
        this._cgModel = cg;
        this._erModel = er;
        this._dbModel = db;
    }

    public CGModel getCGModel() {
        return _cgModel;
    }

    void setCGModel(CGModel cgModel) {
        this._cgmodel = cgModel;
    }

    public CGModel getCgModel() {
        return _cgModel;
    }

    public CGModel getCgmodel() {
        return _cgmodel;
    }

    public Database getDbModel() {
        return _dbModel;
    }

    public ERModel getErModel() {
        return _erModel;
    }

    void reloadModels() {
         _dbModel = Utils.XMLtoModel(dbModelFile);
        _erModel = Utils.XMLToERModel(entityModelFile);
        _erModel.setDatabaseModel(_dbModel);
        _cgModel = CGModel.readFromFile(codeGenerationFile);


        for (CGForm form: _cgModel.getForms())
        {
            if (form.getEntity()==null)
            {
                EREntity e = _erModel.findEntity(form.getEntityName());
                form.setEntity(e);

                for (CGField field: form.getFields() )
                {
                    if (field.getAttrib()==null)
                    {
                        ERAttrib att = e.findAttrib(field.getAttribName());
                        field.setAttrib(att);
                    }
                }
            }
        }
    }


}
